package Entities; //done

import Server.SQL_Manager;
import Client.ConnectionToServer;

/**
 * This class controls Product  details and functions(Add/Update/Delete).
 * @author ameer
 * @author waleed
 * @author bshara
 * @author kamil
 */
public class Product{
	
	/** Product ID */
	private long ProdID;
	
	/** Product Price */
	private double ProdPrice;
	
	/** Product Name */
	private String ProdName;
	
	/** Product Description */
	private String ProdDescription;
	
	/** Product Picture Link */
	private String ProdPic;
	
	/**
	 * Default constructor
	 */
	public Product(){
		this.ProdID = 0;
		this.ProdPrice = 0;
		this.ProdName = null;
		this.ProdDescription = null;
		this.ProdPic = null;
	}
    
    /**
     * @param ProdID Product ID
	 * @param ProdPrice Product Price
	 * @param ProdName Product Name
	 * @param ProdDescription Product Description
	 * @param CategoryID Product CategoryID
	 */
	public Product(double ProdPrice, String ProdName, String ProdDescription, long ProdID, String ProdPic) {
		super();
		this.ProdPrice = ProdPrice;
		this.ProdName = ProdName;
		this.ProdDescription = ProdDescription;
		this.ProdID = ProdID;
		this.ProdPic = ProdPic;
	}

	/**
	 * @return the ProdID
	 */
	public long getProdID(){
		return this.ProdID;
	}
	
	/**
	 * @param prodID the product ID to set
	 */
	public void setProdID(long prodID){
		this.ProdID = prodID;
	}
	
	/**
	 * @return the ProdPrice
	 */
	public double getPrice(){
		return ProdPrice;
	}

	/**
	 * @param ProdPrice the product price to set
	 */
	public void setPrice(double ProdPrice){
		this.ProdPrice = ProdPrice;
	}

	/**
	 * @return the ProdName
	 */
	public String getName(){
		return ProdName;
	}

	/**
	 * @param ProdName the product name to set
	 */
	public void setName(String ProdName){
		this.ProdName = ProdName;
	}

	/**
	 * @return the ProdDescription
	 */
	public String getDescription(){
		return this.ProdDescription;
	}

	/**
	 * @param ProdDescription the product description to set
	 */
	public void setDescription(String ProdDescription){
		this.ProdDescription = ProdDescription;
	}
	
	/**
	 * @return the ProdPic
	 */
	public String getProdPic(){
		return this.ProdPic;
	}

	/**
	 * @param ProdPic the product picture link to set
	 */
	public void setProdPic(String ProdPic){
		this.ProdPic = ProdPic;
	}

	/**
	 * This function returns product details as String Array
	 */
	public String[] toArrString(){
		try{
			String[] productarr = new String[5];
			productarr[0] =  String.valueOf(getPrice());
			productarr[1] = this.getName();
			productarr[2] = this.getDescription();
			productarr[3] = String.valueOf(getProdID());
			productarr[4] = this.getProdPic();
			return productarr;
		}
		catch(Exception e){
			return null;
		}
	}
	
	/**
	 * This function loads a product from DB
	 * @param prodNum the id of the product to load
	 * @return true on success, false on fail
	 * @throws ClientNotInitializesException
	 */
	public boolean loadProduct(long prodNum){		
		ConnectionToServer connectiontoserver = new ConnectionToServer();
		try{
			String[] productinfo = (String[]) connectiontoserver.loadProduct(prodNum);
			if (productinfo == null)
				return false;	
			setPrice(Double.parseDouble(productinfo[0]));
			setName(productinfo[1]);
			setDescription(productinfo[2]);
			setProdID(Long.parseLong(productinfo[3]));
			return true;
		}
		catch(Exception e){
			return false;
		}				
	}
	
	/**
	 * This function updates a product in the DB
	 * @return String on success, null on fail
	 * @throws ClientNotInitializesException
	 */
	public String updateProduct(){		
		ConnectionToServer connectiontoserver = new ConnectionToServer();
		try{
			Object ServerMessage = connectiontoserver.updateProduct(this);
			if (ServerMessage == null)
				return null;
			return (String)ServerMessage;
		}
		catch(Exception e){
			return null;
		}				
	}
	
	/**
	 * This function insert this product to data base
	 */
	public boolean insertToDataBase(){
		try{
			SQL_Manager sql_Manager = new SQL_Manager();
			return (sql_Manager.insertProduct(getPrice(), getName(), getDescription(), getProdID(), getProdPic()));
		}		
		catch (Exception except){
			return false;
		}	
	}
	
	/**
	 * This function load product (to this) from data base
	 */
	public boolean loadFromDataBase(long prodNum){
		try{
			SQL_Manager sql_Manager = new SQL_Manager();
			String[] arrofstring = sql_Manager.searchForProduct(prodNum);
			if 	(arrofstring != null){
				setPrice(Double.parseDouble(arrofstring[0]));
				setName(arrofstring[1]);
				setDescription(arrofstring[2]);
				setProdID(Long.parseLong(arrofstring[3]));
				return true;
			}
			return false;
		}
		catch(Exception excp){
			return false;			
		}			
	}
	
	/**
	 * This method update the data base to product new details 
	 * @return true on success, else false
	 */
	public boolean ServerUpdateProduct(){
		try {
			SQL_Manager sql_Manager = new SQL_Manager();
			return (sql_Manager.updateProduct(getPrice(), getName(), getDescription(), getProdID(), getProdPic()));
		}
		catch (Exception except){
			return false;
		}		
	}
}
